<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>SQL 到 Mongo 的对应表</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongo.writeconcerns.html">Write Concerns</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongo.connecting.html">链接服务器</a></div>
 <div class="up"><a href="mongo.manual.html">手册</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="mongo.sqltomongo" class="chapter">
 <h1>SQL 到 Mongo 的对应表</h1>

 <p class="para">
  这个列表是 PHP 版本的
  <a href="http://docs.mongodb.org/manual/reference/sql-comparison/" class="link external">&raquo;&nbsp;SQL to Mongo</a>
  对应表（在MongoDB官方手册中有更加通用的版本）。
 </p>
 <p class="para">
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>SQL查询语句</th>
      <th>Mongo查询语句</th>
     </tr>

    </thead>


    <tbody class="tbody">
     <tr>
      <td>
       <em>CREATE TABLE USERS (a Number, b Number)</em>
      </td>
      <td>
       隐式的创建，或 <span class="function"><a href="mongodb.createcollection.html" class="function">MongoDB::createCollection()</a></span>.
      </td>
     </tr>

     <tr>
      <td>
       <em>INSERT INTO USERS VALUES(1,1)</em>
      </td>
      <td>
       <em>$db-&gt;users-&gt;insert(array(&quot;a&quot; =&gt; 1, &quot;b&quot; =&gt; 1));</em>
      </td>
     </tr>

     <tr>
      <td>
       <em>SELECT a,b FROM users</em>
      </td>
      <td>
       <em>$db-&gt;users-&gt;find(array(), array(&quot;a&quot; =&gt; 1, &quot;b&quot; =&gt; 1));</em>
      </td>
     </tr>

     <tr>
      <td>
       <em>SELECT * FROM users WHERE age=33</em>
      </td>
      <td>
       <em>$db-&gt;users-&gt;find(array(&quot;age&quot; =&gt; 33));</em>
      </td>
     </tr>

     <tr>
      <td>
       <em>SELECT a,b FROM users WHERE age=33</em>
      </td>
      <td>
       <em>$db-&gt;users-&gt;find(array(&quot;age&quot; =&gt; 33), array(&quot;a&quot; =&gt; 1, &quot;b&quot; =&gt; 1));</em>
      </td>
     </tr>

     <tr>
      <td>
       <em>SELECT a,b FROM users WHERE age=33 ORDER BY name</em>
      </td>
      <td>
       <em>$db-&gt;users-&gt;find(array(&quot;age&quot; =&gt; 33), array(&quot;a&quot; =&gt; 1, &quot;b&quot; =&gt; 1))-&gt;sort(array(&quot;name&quot; =&gt; 1));</em>
      </td>
     </tr>

     <tr>
      <td>
       <em>SELECT * FROM users WHERE age&gt;33</em>
      </td>
      <td>
       <em>$db-&gt;users-&gt;find(array(&quot;age&quot; =&gt; array(&#039;$gt&#039; =&gt; 33)));</em>
      </td>
     </tr>

     <tr>
      <td>
       <em>SELECT * FROM users WHERE age&lt;33</em>
      </td>
      <td>
       <em>$db-&gt;users-&gt;find(array(&quot;age&quot; =&gt; array(&#039;$lt&#039; =&gt; 33)));</em>
      </td>
     </tr>

     <tr>
      <td>
       <em>SELECT * FROM users WHERE name LIKE &quot;%Joe%&quot;</em>
      </td>
      <td>
       <em>$db-&gt;users-&gt;find(array(&quot;name&quot; =&gt; new MongoRegex(&quot;/Joe/&quot;)));</em>
      </td>
     </tr>

     <tr>
      <td>
       <em>SELECT * FROM users WHERE name LIKE &quot;Joe%&quot;</em>
      </td>
      <td>
       <em>$db-&gt;users-&gt;find(array(&quot;name&quot; =&gt; new MongoRegex(&quot;/^Joe/&quot;)));</em>
      </td>
     </tr>

     <tr>
      <td>
       <em>SELECT * FROM users WHERE age&gt;33 AND age&lt;=40</em>
      </td>
      <td>
       <em>$db-&gt;users-&gt;find(array(&quot;age&quot; =&gt; array(&#039;$gt&#039; =&gt; 33, &#039;$lte&#039; =&gt; 40)));</em>
      </td>
     </tr>

     <tr>
      <td>
       <em>SELECT * FROM users ORDER BY name DESC</em>
      </td>
      <td>
       <em>$db-&gt;users-&gt;find()-&gt;sort(array(&quot;name&quot; =&gt; -1));</em>
      </td>
     </tr>

     <tr>
      <td>
       <em>CREATE INDEX myindexname ON users(name)</em>
      </td>
      <td>
       <em>$db-&gt;users-&gt;ensureIndex(array(&quot;name&quot; =&gt; 1));</em>
      </td>
     </tr>

     <tr>
      <td>
       <em>CREATE INDEX myindexname ON users(name,ts DESC)</em>
      </td>
      <td>
       <em>$db-&gt;users-&gt;ensureIndex(array(&quot;name&quot; =&gt; 1, &quot;ts&quot; =&gt; -1));</em>
      </td>
     </tr>

     <tr>
      <td>
       <em>SELECT * FROM users WHERE a=1 and b=&#039;q&#039;</em>
      </td>
      <td>
       <em>$db-&gt;users-&gt;find(array(&quot;a&quot; =&gt; 1, &quot;b&quot; =&gt; &quot;q&quot;));</em>
      </td>
     </tr>

     <tr>
      <td>
       <em>SELECT * FROM users LIMIT 10 SKIP 20</em>
      </td>
      <td>
       <em>$db-&gt;users-&gt;find()-&gt;limit(10)-&gt;skip(20);</em>
      </td>
     </tr>

     <tr>
      <td>
       <em>SELECT * FROM users WHERE a=1 or b=2</em>
      </td>
      <td>
       <em>$db-&gt;users-&gt;find(array(&#039;$or&#039; =&gt; array(array(&quot;a&quot; =&gt; 1), array(&quot;b&quot; =&gt; 2))));</em>
      </td>
     </tr>

     <tr>
      <td>
       <em>SELECT * FROM users LIMIT 1</em>
      </td>
      <td>
       <em>$db-&gt;users-&gt;find()-&gt;limit(1);</em>
      </td>
     </tr>

     <tr>
      <td>
       <em>EXPLAIN SELECT * FROM users WHERE z=3</em>
      </td>
      <td>
       <em>$db-&gt;users-&gt;find(array(&quot;z&quot; =&gt; 3))-&gt;explain()</em>
      </td>
     </tr>

     <tr>
      <td>
       <em>SELECT DISTINCT last_name FROM users</em>
      </td>
      <td>
       <em>$db-&gt;command(array(&quot;distinct&quot; =&gt; &quot;users&quot;, &quot;key&quot; =&gt; &quot;last_name&quot;));</em>
      </td>
     </tr>

     <tr>
      <td>
       <em>SELECT COUNT(*y) FROM users</em>
      </td>
      <td>
       <em>$db-&gt;users-&gt;count();</em>
      </td>
     </tr>

     <tr>
      <td>
       <em>SELECT COUNT(*y) FROM users where AGE &gt; 30</em>
      </td>
      <td>
       <em>$db-&gt;users-&gt;find(array(&quot;age&quot; =&gt; array(&#039;$gt&#039; =&gt; 30)))-&gt;count();</em>
      </td>
     </tr>

     <tr>
      <td>
       <em>SELECT COUNT(AGE) from users</em>
      </td>
      <td>
       <em>$db-&gt;users-&gt;find(array(&quot;age&quot; =&gt; array(&#039;$exists&#039; =&gt; true)))-&gt;count();</em>
      </td>
     </tr>

     <tr>
      <td>
       <em>UPDATE users SET a=1 WHERE b=&#039;q&#039;</em>
      </td>
      <td>
       <em>$db-&gt;users-&gt;update(array(&quot;b&quot; =&gt; &quot;q&quot;), array(&#039;$set&#039; =&gt; array(&quot;a&quot; =&gt; 1)));</em>
      </td>
     </tr>

     <tr>
      <td>
       <em>UPDATE users SET a=a+2 WHERE b=&#039;q&#039;</em>
      </td>
      <td>
       <em>$db-&gt;users-&gt;update(array(&quot;b&quot; =&gt; &quot;q&quot;), array(&#039;$inc&#039; =&gt; array(&quot;a&quot; =&gt; 2)));</em>
      </td>
     </tr>

     <tr>
      <td>
       <em>DELETE FROM users WHERE z=&quot;abc&quot;</em>
      </td>
      <td>
       <em>$db-&gt;users-&gt;remove(array(&quot;z&quot; =&gt; &quot;abc&quot;));</em>
      </td>
     </tr>

    </tbody>
   
  </table>

 </p>
</div>
<hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="mongo.writeconcerns.html">Write Concerns</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="mongo.connecting.html">链接服务器</a></div>
 <div class="up"><a href="mongo.manual.html">手册</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
